import { FormSchema } from '/@/components/Form';
import { BasicColumn } from '/@/components/Table';
import { getAllRoleList } from '/@/api/sys/role';

export const searchFormSchema: FormSchema[] = [
  {
    field: 'username',
    component: 'Input',
    label: '账号',
    colProps: {
      md: 6,
      sm: 8,
    },
  },
  {
    field: 'gender',
    component: 'Select',
    label: '性别',
    colProps: {
      md: 6,
      sm: 8,
    },
    componentProps: {
      options: [
        { label: '男性', value: 1 },
        { label: '女性', value: 2 },
      ],
    },
  },
  {
    field: 'name',
    component: 'Input',
    label: '真实名字',
    colProps: {
      md: 6,
      sm: 8,
    },
  },
  {
    field: 'enableStatus',
    component: 'Select',
    label: '用户状态',
    colProps: {
      md: 6,
      sm: 8,
    },
    componentProps: {
      options: [
        { label: '启用', value: 1 },
        { label: '禁用', value: 2 },
      ],
    },
  },
];

export const userColumns: BasicColumn[] = [
  {
    title: '用户账号',
    align: 'center',
    dataIndex: 'username',
    width: 120,
  },
  {
    title: '用户姓名',
    align: 'center',
    width: 100,
    dataIndex: 'name',
  },
  {
    title: '头像',
    align: 'center',
    width: 120,
    dataIndex: 'headPic',
  },
  {
    title: '性别',
    align: 'center',
    width: 80,
    dataIndex: 'gender',
  },
  {
    title: '生日',
    align: 'center',
    width: 100,
    dataIndex: 'birthday',
  },

  {
    title: '状态',
    align: 'center',
    width: 80,
    dataIndex: 'enableStatus',
  },
];

export const userFormSchemas: FormSchema[] = [
  {
    field: 'username',
    component: 'Input',
    label: '用户账号',
    required: true,
  },
  {
    field: 'password',
    component: 'InputPassword',
    label: '登陆密码',
    required: true,
  },
  {
    field: 'name',
    component: 'Input',
    label: '用户姓名',
    required: true,
  },
  {
    field: 'roleIds',
    label: '角色分配',
    component: 'ApiSelect',
    componentProps: {
      mode: 'multiple',
      api: getAllRoleList,
      labelField: 'name',
      valueField: 'id',
      valueType: 'Array',
    },
  },
  {
    field: 'headPic',
    label: '头像',
    component: 'JImageUpload',
  },
  {
    field: 'birthday',
    label: '生日',
    component: 'DatePicker',
    componentProps: {
      valueFormat: 'YYYY-MM-DD',
    },
  },
  {
    field: 'enableStatus',
    component: 'Select',
    label: '性别',
    defaultValue: 1,
    required: true,
    componentProps: {
      allowClear: false,
      options: [
        { label: '男', value: 1 },
        { label: '女', value: 2 },
      ],
    },
  },
  {
    field: 'email',
    component: 'Input',
    label: '邮箱',
    rules: [{ required: true, type: 'email' }],
  },
];

export const userPasswordFormSchemas: FormSchema[] = [
  {
    field: 'username',
    component: 'Input',
    label: '用户账号',
    required: true,
  },
  {
    field: 'password',
    component: 'InputPassword',
    label: '登陆密码',
    required: true,
  },
  {
    field: 'confirmpassword',
    component: 'InputPassword',
    label: '确认密码',
    dynamicRules: ({ model }) => {
      return [
        {
          required: true,
        },
        {
          validator: (_, value) => {
            if (value !== model.password) {
              return Promise.reject('两次输入的密码不一致');
            } else {
              return Promise.resolve();
            }
          },
          trigger: 'blur',
        },
      ];
    },
  },
];
